package finance

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/finance"
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var OrderGroup = core.Group("/finance/recharge", newOrderHandler, regOrder, middleware.TokenAuth())

func newOrderHandler(srv finance.IRechargeOrderService) *orderHandler {
	return &orderHandler{srv: srv}
}

func regOrder(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *orderHandler) {
		rg.GET("/lists", handle.list)
		rg.POST("/refund", handle.refund)

	})
}

type orderHandler struct {
	srv finance.IRechargeOrderService
}

// list order列表
func (h orderHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.RechargeOrderListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := h.srv.List(c, page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

// 充值订单退款
func (h orderHandler) refund(c *gin.Context) {
	var refundReq req.RechargeOrderRefundReq

	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &refundReq)) {
		return
	}
	err := h.srv.Refund(c, refundReq)
	response.CheckAndResp(c, err)
}
